package dao;

import model.Blog;
import utils.DBUtils;

import javax.rmi.CORBA.Util;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/4/1 22:48
 */
public class BlogDao {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    /**
     * 获取所有的博客列表
     *
     * @return
     */
    public List<Blog> selectAll() {
        List<Blog> list = new ArrayList<>();
        connection = DBUtils.getConnection();
        if (connection == null) {
            return null;
        }
        try {
            String sql = " select * from blog where delete_flag=0 ";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Blog blog = new Blog();
                String content = resultSet.getString("content");
                if (content.length() >50){
                    content =content.substring(0,50)+"...";
                }
                blog.setId(resultSet.getInt("id"))
                        .setTitle(resultSet.getString("title"))
                        .setContent(content)
                        .setUserId(resultSet.getInt("user_id"))
                        .setDeleteFlag(resultSet.getInt("delete_flag"))
                        .setCreateTime(resultSet.getTimestamp("create_time"))
                        .setUpdateTime(resultSet.getTimestamp("update_time"));
                list.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection, statement, resultSet);
        }
        return list;
    }

    /**
     * 根据博客ID,获取博客的信息
     */
    public Blog getBlogByID(int blogId) {
        connection = DBUtils.getConnection();
        if (connection == null) {
            return null;
        }
        Blog blog = null;
        try {
            String sql = "select * from blog where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                blog = new Blog();
                blog.setId(resultSet.getInt("id"))
                        .setTitle(resultSet.getString("title"))
                        .setContent(resultSet.getString("content"))
                        .setUserId(resultSet.getInt("user_id"))
                        .setDeleteFlag(resultSet.getInt("delete_flag"))
                        .setCreateTime(resultSet.getTimestamp("create_time"))
                        .setUpdateTime(resultSet.getTimestamp("update_time"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection, statement, resultSet);
        }
        return blog;
    }

    /**
     * 插入博客信息
     */
    public void insertBlog(Blog blog) {
        connection = DBUtils.getConnection();
        if (connection == null) {
            return;
        }
        try {
            String sql = "insert  into blog(`title`,`content`,`user_id`) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection, statement, null);
        }
    }

    /**
     * 删除博客信息
     */
    public void deleteBlog(Integer blogId) {
        connection = DBUtils.getConnection();
        if (connection == null){
            return;
        }

        try {
            String sql = "update  blog set delete_flag = '1' where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            statement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.close(connection,statement,null);
        }
    }
}
